package steps
{
	
	import laya.ani.bone.EventData;
	import laya.d3.math.Vector2;
	import laya.events.Event;
	import laya.utils.Handler;
	
	import ui.steps.dragTuziUI;
	
	public class DragAndMoveRabbit extends dragTuziUI
	{
		/**
		 *当前动画索引 
		 */		
		private var index:Number=0;
		private var isMouseDown:Boolean;
		public function DragAndMoveRabbit()
		{
			super();
		}
		
		override public function onAwake():void
		{
			// TODO Auto Generated method stub
			super.onAwake();
			skeleton.load("p1/gdtuzi.sk",Handler.create(this,parseComplete));
		}
		private function parseComplete():void
		{
			// TODO Auto Generated method stub
			skeleton.play("daiji",true);
			hideZhishi();
			event("resReady");
		}
		public function InitListener():void
		{
			this.on(Event.MOUSE_DOWN,this,onThisdown);
			dragRect.on(Event.MOUSE_DOWN,this,onThisdown);
			showZhishi();
		}
		public function hideZhishi():void{
			zhishi.visible=false;
		}
		public function showZhishi():void{
			zhishi.visible=true;
		}
		private var dragRight:Boolean=false;
		private var p:Vector2=new Vector2();
		private function onThisdown(e:Event):void
		{
			// TODO Auto Generated method stub
			trace("mousedown:"+e.currentTarget.name);
			if(e.target.name!="dragRect") event('WRONGDRAG');
			else{
				dragRight=true;
				//				skeleton.on(Event.LABEL,this,onLable);
				event("BEGIN_DRAG");
				skeleton.play("zhua",false,true);
				zhishi.visible=false;
				Laya.stage.on(Event.MOUSE_MOVE,this,onMove);
				Laya.stage.on(Event.MOUSE_UP,this,onUp);
				Laya.stage.on(Event.MOUSE_OUT,this,onUp);
				p.x=mouseX;
				p.y=mouseY;
			}
		}
		
		private function onLable(e:EventData):void
		{
			// TODO Auto Generated method stub
			trace("lable:"+e.name);
			if(e.name=="end"){
				skeleton.off(Event.LABEL,this,onLable);
				skeleton.play("daiji",true);
				event("END_DRAG");
			}
		}
		public function onUp():void
		{
			// TODO Auto Generated method stub
			if(dragRight){
				trace("playFang");
				skeleton.on(Event.LABEL,this,onLable);
				skeleton.play("fangxialai",false,true);
				dragRight=false;
			}
			zhishi.visible=true;
			Laya.stage.off(Event.MOUSE_MOVE,this,onMove);
			Laya.stage.off(Event.MOUSE_UP,this,onUp);
			Laya.stage.off(Event.MOUSE_OUT,this,onUp);
		}
		private function onMove():void
		{
			// TODO Auto Generated method stub
			if(skeleton.player.currentAnimationClipIndex!=2){
				skeleton.play("zhuaqilai",false,false);
			}
			event("DRAGING");
		}
		/**
		 *DB动画加载出错 
		 * 
		 */		
		private function onParseError():void
		{
			// TODO Auto Generated method stub
			trace('骨骼动画加载出错');
		}
		
		public function onHit():void
		{
			// TODO Auto Generated method stub
			dragRect.destroy();
			this.off(Event.MOUSE_DOWN,this,onThisdown);
			skeleton.offAll();
			Laya.stage.off(Event.MOUSE_MOVE,this,onMove);
			skeleton.play("daiji",false,true);
			zhishi.visible=false;
			zhishi.stop();
		}
	}
}